﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace oqis.core.dbaccess
{
    public abstract class OQISDBAccessBase : IOQISDBAccess, IDisposable
    {
        #region 类成员变量

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        protected IDbConnection _dbConnection = null;

        /// <summary>
        /// 数据库操作对象
        /// </summary>
        protected IDbCommand _dbCommand = null;

        /// <summary>
        /// 数据库适配器
        /// </summary>
        protected IDbDataAdapter _dbAdapter = null;
       
        #endregion

        #region 类构建与初始化


        #endregion

        #region IDBAccess 成员

        /// <summary>
        /// 数据适配器
        /// </summary>
        public IDbDataAdapter DataAdapter { get { return _dbAdapter; } }

        /// <summary>
        /// 数据库操作对象
        /// </summary>
        public IDbCommand DbCommand { get { return _dbCommand; } }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        public IDbConnection DbConnection { get { return _dbConnection; } }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string DBConnectionString { get { return _dbConnection.ConnectionString; } }

        /// <summary>
        /// 数据库操作命令
        /// </summary>
        public string CommandText { get { return _dbCommand.CommandText; } set { _dbCommand.CommandText = value; } }

        /// <summary>
        /// 数据库类型
        /// </summary>
        public virtual qiaoDBMSType DBMSType { get { return  qiaoDBMSType.None; } }

        #endregion

        #region IDBAccess方法

        /// <summary>
        /// 执行数据库操作，返回受影响的记录数
        /// </summary>
        /// <param name="sql">数据库操作语句</param>
        /// <returns>成功刚返回受影响的记录数，执行失败返回-1</returns>
        public int ExcuteNoneQuery(string sql)
        {
            // 判断
            if (this._dbCommand == null) return -1;

            // 操作命令语句
            this._dbCommand.CommandText = sql;

            // 判断数据库状态
            if (this._dbConnection.State == ConnectionState.Closed) this._dbConnection.Open();

            // 执行数据库操作
            int result = this._dbCommand.ExecuteNonQuery();

            // 关闭数据库连接
            if (this._dbConnection.State != ConnectionState.Closed) this._dbConnection.Close();

            // 返回结果
            return result;
        }

        /// <summary>
        /// 执行数据库操作，返回第一条记录
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public object ExcuteScalar(string sql)
        {
            // 判断
            if (this._dbCommand == null) return null;

            // 操作命令语句
            this._dbCommand.CommandText = sql;

            // 判断数据库状态
            if (this._dbConnection.State == ConnectionState.Closed) this._dbConnection.Open();

            // 执行数据库操作
            object result = this._dbCommand.ExecuteScalar();

            // 关闭数据库连接
            if (this._dbConnection.State != ConnectionState.Closed) this._dbConnection.Close();

            // 返回结果
            return result;
        }

        /// <summary>
        /// 获取数据表
        /// </summary>
        /// <param name="sql">数据库操作语句</param>
        /// <returns>返回数据表对象</returns>
        public DataTable GetDataTable(string sql)
        {
            // 获取数据集
            DataSet ds = GetDataSet(sql);

            // 判断
            if (ds == null || ds.Tables.Count < 1) return null;

            // 返回结果
            return ds.Tables[0];
        }

        /// <summary>
        /// 获取数据集
        /// </summary>
        /// <param name="sql">数据库操作语句</param>
        /// <returns></returns>
        public DataSet GetDataSet(string sql)
        {
            try
            {
                // 构建数据库访问适配器
                if (this._dbAdapter == null) return null;
                _dbAdapter.SelectCommand.CommandText = sql;
                // 获取数据
                DataSet ds = new DataSet();
                this._dbAdapter.Fill(ds);

                // 返回结果
                return ds;
            }
            catch { return null; }
        }

        /// <summary>
        /// 更新数据表
        /// </summary>
        /// <param name="tableName">更新的数据表</param>
        /// <param name="table">表对象</param>
        /// <returns></returns>
        public virtual bool UpdateTable(string tableName, DataTable table)
        {
            return false;
        }

        #endregion

        #region 初始化与释放

        /// <summary>
        /// 必须重写项--初始化数据库对象
        /// </summary>
        public virtual void InitializeDBAccess()
        {

        }

        /// <summary>
        /// 释放资源--关闭连接
        /// </summary>
        public void Dispose()
        {
            // 释放
            if (_dbCommand != null) _dbCommand.Dispose();
            if (_dbConnection != null || _dbConnection.State != ConnectionState.Closed)
            {
                // 关闭连接
                _dbConnection.Close();
                _dbConnection.Dispose();
            }
        }
       
        #endregion
    }
}
